package gov.kqqzyyy.monkey.orderpad.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.Collections;
import java.util.List;

import gov.kqqzyyy.monkey.orderpad.R;
import gov.kqqzyyy.monkey.orderpad.object.DC_CPXX;
import gov.kqqzyyy.monkey.orderpad.object.MenuDetail;
import gov.kqqzyyy.monkey.orderpad.sectionedrecyclerview.SectionedRecyclerViewAdapter;
import gov.kqqzyyy.monkey.orderpad.ui.OrderMenuActivity;


/**
 * Created by Monkey on 2017/9/4.
 * 点餐菜单适配器
 */

public class OrderGroupDetailAdapter extends SectionedRecyclerViewAdapter<OrderGroupDetailAdapter.HeaderHolder, OrderGroupDetailAdapter.ContentHolder, RecyclerView.ViewHolder> {

    private final Context mContext;
    private final LayoutInflater mInflater;
    private List<MenuDetail> list;

    public OrderGroupDetailAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<MenuDetail> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    protected int getSectionCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    protected int getItemCountForSection(int section) {
        if (list != null && list.get(section) != null && list.get(section).getDishList() != null) {
            return list.get(section).getDishList().size();
        }
        return 0;
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    protected HeaderHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        return new HeaderHolder(mInflater.inflate(R.layout.item_ordermenu_header, parent, false));
    }

    @Override
    protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected ContentHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new ContentHolder(mInflater.inflate(R.layout.item_ordermenu_content, parent, false));
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    protected void onBindSectionHeaderViewHolder(HeaderHolder holder, int section) {
        MenuDetail menuDetail = list.get(section);
        if (menuDetail != null) {
            holder.groupTitleText.setText(menuDetail.getGroupTitle());
            holder.radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
                List<DC_CPXX> dishList = menuDetail.getDishList();
                switch (checkedId) {
                    case R.id.rb_orderbyname:
                        Collections.sort(dishList, (arg0, arg1) -> arg0.getID() - arg1.getID());
                        break;
                    case R.id.rb_orderbyprice:
                        Collections.sort(dishList, (arg0, arg1) -> Double.compare(arg0.getJG(), arg1.getJG()));
                        break;
                }
                menuDetail.setDishList(dishList);
                notifyDataSetChanged();
            });
        }
    }

    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {

    }

    @Override
    protected void onBindItemViewHolder(final ContentHolder holder, final int section, final int position) {
        MenuDetail menuDetail = list.get(section);
        if (menuDetail != null) {
            List<DC_CPXX> dishList = menuDetail.getDishList();
            if (dishList != null) {
                final DC_CPXX dishInfo = dishList.get(position);
                if (dishInfo != null) {
                    if (dishInfo.getTP() != null && !dishInfo.getTP().equals("")) {//加载菜品图片
                        holder.dishSampleIMG.setImageURI(Uri.parse(dishInfo.getTP()));
                    } else {
                        holder.dishSampleIMG.setImageResource(R.drawable.ic_no_image);
                    }
                    holder.dishName.setText(dishInfo.getCPMC());
                    holder.dishLX.setText(dishInfo.getLX());
//                    String kcsl = "库存数量：" + df.format(dishInfo.getKCSL());
//                    holder.dishSaleInfo.setText(kcsl);
                    String price = "￥" + dishInfo.getJG();
                    holder.dishPrice.setText(price);

                    String orderNum = String.valueOf(dishInfo.getOrderNum());
                    if (orderNum.equals("0")) {
                        holder.dishNum.setVisibility(View.INVISIBLE);
                        holder.dishNumMinusIMG.setVisibility(View.INVISIBLE);
                    } else {
                        holder.dishNum.setVisibility(View.VISIBLE);
                        holder.dishNumMinusIMG.setVisibility(View.VISIBLE);
                        holder.dishNum.setText(orderNum);
                    }

                    holder.dishNumPlusIMG.setOnClickListener(view -> {
                        dishInfo.setOrderNum(dishInfo.getOrderNum() + 1);
                        ((OrderMenuActivity) mContext).changeDishNum(section, position);
                        notifyDataSetChanged();
                    });
                    holder.dishNumMinusIMG.setOnClickListener(view -> {
                        dishInfo.setOrderNum(dishInfo.getOrderNum() - 1);
                        ((OrderMenuActivity) mContext).changeDishNum(section, position);
                        notifyDataSetChanged();
                    });

                    holder.dishSampleIMG.setOnLongClickListener(v -> {
                        Toast.makeText(mContext, "long click", Toast.LENGTH_SHORT).show();
                        return false;
                    });

                }
            }
        }
    }

    static class HeaderHolder extends RecyclerView.ViewHolder {

        private TextView groupTitleText;
        private RadioGroup radioGroup;

        HeaderHolder(View itemView) {
            super(itemView);
            initView();
        }

        private void initView() {
            groupTitleText = itemView.findViewById(R.id.header_group_name);
            radioGroup = itemView.findViewById(R.id.header_order_radiogroup);
        }
    }

    static class ContentHolder extends RecyclerView.ViewHolder {
        //        private ImageView dishSampleIMG;
        private SimpleDraweeView dishSampleIMG;
        private TextView dishName;
        private TextView dishLX;//菜品类型
        //        private TextView dishSaleInfo;
        private TextView dishPrice;
        private ImageView dishNumMinusIMG;
        private TextView dishNum;
        private ImageView dishNumPlusIMG;

        ContentHolder(View itemView) {
            super(itemView);
            initView();
        }

        private void initView() {
            dishSampleIMG = itemView.findViewById(R.id.content_dish_img);
            dishName = itemView.findViewById(R.id.content_dish_name);
            dishLX = itemView.findViewById(R.id.content_dish_lx);
//            dishSaleInfo = itemView.findViewById(R.id.content_sale_info);
            dishPrice = itemView.findViewById(R.id.content_dish_price);
            dishNumMinusIMG = itemView.findViewById(R.id.content_num_minus);
            dishNum = itemView.findViewById(R.id.content_dish_num);
            dishNumPlusIMG = itemView.findViewById(R.id.content_num_plus);
        }

    }
}
